<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户信息</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/static/admin/style.css}" media="all">
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body" style="padding: 15px;">
                <form class="layui-form layui-form-pane" action="">
                    <input name="id" th:value="${obj?.id}" class="layui-input layui-hide"/>
                    <div class="layui-form-item">
                        <label class="layui-form-label">图片名称</label>
                        <div class="layui-input-block">
                            <input type="text" id="imgname" name="imgname" th:value="${obj?.imgname}" lay-verify="required" placeholder="请输入用户名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">类别表编号</label>
                        <div class="layui-input-block">
                            <select name="cid" id="cid"></select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">存放路径</label>
                        <div class="layui-input-block">
                            <input type="text"  id="path" name="path" th:value="${obj?.path}" lay-verify="required"  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">存放路径</label>
                        <div class="layui-input-block">
                            <input type="text"  id="serverpath" name="serverpath" th:value="${obj?.serverpath}" lay-verify="required"  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">上传按钮</label>
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn" id="test1">
                                <i class="layui-icon">&#xe67c;</i>上传图片
                            </button>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">图片预览</label>
                        <div class="layui-input-block">
                            <img id="image" style="height: 200px;width: 300px">
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <input type="button" lay-submit lay-filter="LAY-front-submit" id="LAY-front-submit" value="确认">
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script th:src="@{/static/layui/layui.js}"></script>
    <script>
        layui.use(['form'],function () {
            var $ = layui.$;
            var form = layui.form;

            //当前供应商的值
            var currentCid = '[[${obj?.cid}]]';
            //获取供应商列表
            $.ajax({
                url: '/classes/classesInfoList',
                dataType:'json',
                type:'post',
                cache:false,
                success:function (data) {
                    $('#cid').prepend("<option value=''>请选择</option>");
                    for(var i=0;i<data.data.length;i++){
                        if(data.data[i].id==currentCid){
                            $('#cid').append("<option selected value='"+data.data[i].id+"'>"+data.data[i].cname+"</option>");
                        }else{
                            $('#cid').append("<option value='"+data.data[i].id+"'>"+data.data[i].cname+"</option>");
                        }
                    }
                    form.render();          //下拉列表渲染，把内容加载进去
                }
            });
        });
        layui.use('upload', function () {
            let upload = layui.upload;
            let $ = layui.$;
            //执行实例
            let uploadInst = upload.render({
                elem: '#test1' //绑定元素
                , url: '/img/upload' //上传接口
                , done: function (res) {
                    if (res.code == 1) {
                        layer.msg("上传成功");
                        //获得图片服务器访问路径
                        let src = res.data.src;
                        //将图片进行回显
                        $("#image").attr("src", src);
                        //将图片的地址赋值给 form的隐藏域
                        $("#path").attr("value", res.data.path);
                        $("#serverpath").attr("value", res.data.src);
                        $("#imgname").attr("value", res.data.filename);
                    } else {
                        layer.msg("上传失败");
                    }

                }
                , error: function () {
                    //请求异常回调
                }
            });
        });
    </script>
</body>
</html>
















